route.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ImportProcessor } from '@/app/lib/excel-import/import-processor';
  3. export async function POST(
  4. request: NextRequest,
  5. { params }: { params: { id: string } }
  6. ) {
  7. try {
  8. const importId = parseInt(params.id);
  9. if (isNaN(importId)) {
  10. return NextResponse.json(
  11. { success: false, error: 'Invalid import ID' },
  12. { status: 400 }
  13. );
  14. }
  15. // Initialize import processor
  16. const processor = new ImportProcessor();
  17. // Validate import before processing
  18. const validation = await processor.validateImport(importId);
  19. if (!validation.valid) {
  20. return NextResponse.json(
  21. { success: false, error: validation.errors.join(', ') },
  22. { status: 400 }
  23. );
  24. }
  25. // Start processing in background
  26. processor.processImport(importId).catch(error => {
  27. console.error('Import processing failed:', error);
  28. });
  29. return NextResponse.json({
  30. success: true,
  31. message: 'Import process started successfully',
  32. importId
  33. });
  34. } catch (error) {
  35. console.error('Error triggering import:', error);
  36. return NextResponse.json(
  37. { success: false, error: 'Failed to trigger import' },
  38. { status: 500 }
  39. );
  40. }
  41. }